package com.ahao.aggregation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ahao.aggregation.AHSDKManager;
import com.ahao.aggregation.FloatViewManager;
import com.ahao.aggregation.entry.UserInfo;
import com.ahao.aggregation.utils.ScreenUtils;

public class CustomPersonalDialog {

    private static Dialog dialog;

    public static void showPersonalDialog(Context context) {
        // 创建一个线性布局
        LinearLayout layout = new LinearLayout(context);
        layout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ScreenUtils.getDisplayMetrics(context).heightPixels));
        layout.setOrientation(LinearLayout.VERTICAL);
        // 设置layoutpandding 为16
        layout.setPadding(16, 0, 16, 0);
        // 设置layout背景为半透明
        layout.setBackgroundColor(0x7f000000);
        // 创建一个TextView
        TextView textView = new TextView(context);
        String username = UserInfo.getInstance().getLoginResult() != null ? UserInfo.getInstance().getLoginResult().getUsername() : "请先登录";
        textView.setText(String.format("用户名:%s", username));
        textView.setTextSize(20);
        textView.setTextColor(0xffffffff);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.addView(textView);
        // 创建一个button
        Button button = new Button(context);
        button.setText("退出登录");
        button.setTextSize(20);
        button.setTextColor(0xffffffff);
        button.setGravity(Gravity.CENTER);
        button.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfo.getInstance().setLoginResult(null);
                AHSDKManager.getInstance().getSDKListener().onLogoutResult(0, "退出登录成功");
                FloatViewManager.getInstance().hideFloatView();
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });

        layout.addView(button);

        // 显示对话框
        dialog = new Dialog(context){
            @Override
            public void dismiss() {
                // 出场动画从右边退出
                getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
                super.dismiss();
                dialog = null;
            }
            // 进场动画从左边进入，出场动画从右边退出
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
            }

            @Override
            public void onBackPressed() {
                super.onBackPressed();
                dialog = null;
            }

        };
        dialog.setContentView(layout);
        // 设置dialog弹出的位置
        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.CENTER);
        // 设置高度铺满屏幕
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT,
                ScreenUtils.getDisplayMetrics(context).heightPixels);
        // 设置padding为0
        dialog.getWindow().getDecorView().setPadding(0, 0, 0, 0);
        // 设置背景透明
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
    }
}
